﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Data;
using System.Data.SqlClient;
using Deployment.Data.SQL.Connections.UI;

namespace Deployment.Data.SQL.Connections
{
    public class SQLServerConnection :AbstractSQLConnection
    {
        public SQLServerConnection() :base()
        {
            base._TypeDescription = "SQL Server Connection";
        }

        protected override bool performConnectionAccessChecks(SqlConnection connection, out string message)
        {
            connection.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT IS_SRVROLEMEMBER('sysadmin')", connection))
            {
                int? result = cmd.ExecuteScalar() as int?;
                bool value = (result != null && result == 1);
                message = value ? null : "Authenticated user must be a member of the 'SysAdmin' Server Role.";
                return value;
            }
        }

        protected override UserControl createControl()
        {
            return new SQLServerControl();
        }

        protected override void subClassOnDataUpdated(){}

        protected override bool DataValid()
        {
            return true;
        }
    }
}
